class First{
    constructor(){
     this.changeName();
     this.logout();
    }

     //登陆后显示用户名
async changeName(){
    let userId = localStorage.getItem('user_id');
    // console.log(userId);
    axios.defaults.headers['authorization'] = 'application/x-www-form-urlencoded';
   let { status,data } = await axios.get('http://localhost:8888/users/info?id='+userId);
//    console.log(data.info.nickname);
     let username = data.info.nickname;
     if(status!=200) return
     let user = this.$('.username');
   //   console.log(user);
    let param = `${ username }<i class="iconfont icon-jiantou_xiangxia1"></i>`
     user.innerHTML = param;
     let token = localStorage.getItem('token');
   //   console.log(token);
     if(!token){
        user.innerHTML = '请登录';
        location.assign('./login.html?returnUrl=./search.html')
     }
}
 //退出登陆
 logout(){
    this.$('.goHome').addEventListener('click',this.userLogout);
    // let res = this.$('.gohome');
    // console.log(res);
 }
  userLogout=()=>{
    // console.log(11111);
    // 发送ajax请求
    let userId = localStorage.getItem('user_id');
    console.log(userId);
    let res = axios.get('http://localhost:8888/users/logout?id='+userId).then(()=>{
       location.assign('./index.html')
    });
    // console.log(res);
 }
//封装获取节点的方法
$(ele){
    let res = document.querySelectorAll(ele);
    return res.length==1 ? res[0] : res;
}
}

new First;